Skip to content

[Tracing] Adds SQS and LambdaContext Trace extraction#105

Merged
Czechh merged 13 commits into
mainfrom
sergio.prada/extract-context-from-sqs-lambda-client-context
Jan 6, 2021
Merged

[Tracing] Adds SQS and LambdaContext Trace extraction#105
Czechh merged 13 commits into
mainfrom
sergio.prada/extract-context-from-sqs-lambda-client-context

Conversation

@Czechh
Copy link
Copy Markdown
Contributor

@Czechh Czechh commented Dec 17, 2020

What does this PR do?

Following the dd-trace-py 0.45.0 release this PR adds support for extracting the trace data from SQS and the Lambda Context object

Motivation

Testing Guidelines

Additional Notes

Types of Changes

  • Bug fix
  • New feature
  • Breaking change
  • Misc (docs, refactoring, dependency upgrade, etc.)

Check all that apply

  • This PR's description is comprehensive
  • This PR contains breaking changes that are documented in the description
  • This PR introduces new APIs or parameters that are documented and unlikely to change in the foreseeable future
  • This PR impacts documentation, and it has been updated (or a ticket has been logged)
  • This PR's changes are covered by the automated tests
  • This PR collects user input/sensitive content into Datadog
  • This PR passes the integration tests (ask a Datadog member to run the tests)

@Czechh Czechh requested a review from a team as a code owner December 17, 2020 14:19
Comment thread datadog_lambda/tracing.py Outdated
parent_id = lowercase_headers.get(TraceHeader.PARENT_ID)
sampling_priority = lowercase_headers.get(TraceHeader.SAMPLING_PRIORITY)
if "headers" in event:
trace_id, parent_id, sampling_priority = extract_context_from_http_event(event)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc: @DylanLovesCoffee I recall you are touching this function as well, and I actually suggested code changes like what @Czechh does here.

Copy link
Copy Markdown
Contributor

@DylanLovesCoffee DylanLovesCoffee Dec 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I am! This comment? I had changed extract_dd_trace_context here to return the source as well and create the subsegment in a separate function.

Comment thread datadog_lambda/tracing.py Outdated
Comment thread datadog_lambda/tracing.py Outdated
Comment thread datadog_lambda/tracing.py Outdated
return trace_id, parent_id, sampling_priority


def extract_context_from_sqs_event(event):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not hard, does it make sense to add an integration test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified the SQS object to inject trace data ✅

Comment thread datadog_lambda/tracing.py Outdated
Comment thread datadog_lambda/tracing.py Outdated
if "headers" in event:
trace_id, parent_id, sampling_priority = extract_context_from_http_event(event)
elif "Records" in event:
trace_id, parent_id, sampling_priority = extract_context_from_sqs_event(event)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If "Records" is in the event, but we fail to parse, we should probably try extracting from the lambda context next.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the lambda context as a fallback for both headers and SQS events

Comment thread datadog_lambda/tracing.py Outdated


def extract_context_from_lambda_context(lambda_context):
dd_data = lambda_context.client_context.get("custom", {}).get("_datadog", {})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, didn't realize it was possible for some services to add metadata to the context.

{"e": XXXX, "m": "hello.dog", "t": ["team:serverless", "role:hello", "dd_lambda_layer:datadog-python27_2.XX.0"], "v": 1}
{"e": XXXX, "m": "tests.integration.count", "t": ["test:integration", "role:hello", "dd_lambda_layer:datadog-python27_2.XX.0"], "v": 21}
{"traces": [[{"resource": "integration-plugin-dev-async-metrics_python27_with_plugin", "name": "aws.lambda", "service": "aws.lambda", "start": XXXX, "trace_id": "XXXX", "metrics": {"_sampling_priority_v1": 2, "system.pid": XXXX}, "parent_id": "XXXX", "meta": {"runtime-id": "XXXX", "request_id": "XXXX", "cold_start": "false", "datadog_lambda": "2.24.0", "function_arn": "arn:aws:lambda:us-east-1:601427279990:function:integration-plugin-dev-async-metrics_python27_with_plugin", "dd_trace": "0.44.0", "_dd.origin": "lambda", "_dd.parent_source": "xray", "resource_names": "integration-plugin-dev-async-metrics_python27_with_plugin", "function_version": "$LATEST"}, "error": 0, "duration": XXXX, "type": "serverless", "span_id": "XXXX"}]]}
{"traces": [[{"resource": "integration-plugin-dev-async-metrics_python27_with_plugin", "name": "aws.lambda", "service": "aws.lambda", "start": XXXX, "trace_id": "XXXX", "metrics": {"_sampling_priority_v1": 1, "system.pid": XXXX}, "parent_id": "XXXX", "meta": {"runtime-id": "XXXX", "request_id": "XXXX", "cold_start": "false", "datadog_lambda": "X.X.X", "function_arn": "arn:aws:lambda:us-east-1:601427279990:function:integration-plugin-dev-async-metrics_python27_with_plugin", "dd_trace": "X.X.X", "_dd.origin": "lambda", "resource_names": "integration-plugin-dev-async-metrics_python27_with_plugin", "function_version": "$LATEST"}, "error": 0, "duration": XXXX, "type": "serverless", "span_id": "XXXX"}]]}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"_dd.parent_source": "xray" is gone because I added to the SQS input event some trace data :)

…-client-context

* main:
  Update contributing doc (#106)
@Czechh Czechh merged commit 9646080 into main Jan 6, 2021
@Czechh Czechh deleted the sergio.prada/extract-context-from-sqs-lambda-client-context branch January 6, 2021 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants